package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

+------------+
|2020/4/10   |
|14:55       |
+------------+
*/
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{

    @Autowired
    private OrderSettingDao settingDao;
    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //检查该日期是否存在
                long count=settingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //日期存在执行更新操作
                    settingDao.update(orderSetting);
                } else {
                    //日期不存在执行插入操作
                    settingDao.add(orderSetting);
                }
            }
        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String start = date + "-1";
        String end = date + "-31";
        //设置起始和结束时间，查询当月所有预约天数
        Map<String, String> map = new HashMap<>();
        map.put("start", start);
        map.put("end", end);
        //封装查询结果到list并遍历，封装为前端可以解析的格式
        List<OrderSetting> list=settingDao.getOrderSettingByMonth(map);
        List<Map> resultList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map resultMap = new HashMap();
//     前端解析格式 { date: 1, number: 120, reservations: 1 }
                resultMap.put("date", orderSetting.getOrderDate().getDate());
                resultMap.put("number", orderSetting.getNumber());
                resultMap.put("reservations", orderSetting.getReservations());
                resultList.add(resultMap);
            }
        }
        return resultList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询是否该日是否存在
        long count = settingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //日期已设置，修改操作
            settingDao.update(orderSetting);
        } else {
            settingDao.add(orderSetting);
        }
    }
}
